package com.zj.leetcode.easy.tree;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: algorithm
 * @description: 路径总和
 * 112 path-sum
 * @author: Zhang Bo
 * @create: 2022-06-22 10:32:50
 **/
public class PathSum {
    public static void main(String[] args) {
        Solution solution = new PathSum().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        private final Set<Integer> set = new HashSet<>();
        public boolean hasPathSum(TreeNode root, int targetSum) {
            if(root == null){
                return false;
            }

            this.dfs(root, root.val);
            System.out.println(set);
            return set.contains(targetSum);

        }

        private void dfs(TreeNode node, int sum) {
            if(node != null){
                if(node.left == null && node.right == null){
                    set.add(sum);
                }
                if(node.left != null){
                    this.dfs(node.left, sum + node.left.val);
                }

                if(node.right != null){
                    this.dfs(node.right, sum + node.right.val);
                }

            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}